package com.testdemo.common.utils;

import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.chad.library.adapter.base.BaseQuickAdapter;

import java.util.List;

/**
 * Created by cy on 2016/8/10.
 * 分页加载器
 */
public abstract class PageLoader<T> {

    public static final int TYPE_UPTATE = 1;
    public static final int TYPE_NEXT_PAGE = 2;

    //是否需要缓存
    public boolean needCache = false;

    public int pageIndex = -1;

    public int pageSize = 20;

    public List<T> dataList;//数据源

    boolean firstLoad = true;//第一次加载数据

    public RecyclerView.Adapter adapter;

    public void setDataList(List<T> dataList){
        this.dataList = dataList;
    }

    public PageLoader(RecyclerView.Adapter adapter, List<T> dataList, boolean needCache) {
        this.needCache = needCache;
        this.adapter = adapter;
        this.dataList = dataList;
    }

    public abstract void getData(int pageIndex, int pageSize, int getDataType);


    public void initData() {
        firstLoad = true;
        if (needCache) {
            this.dataList.clear();
            try {
                List<T> cacheList=getCache();
                if (cacheList!=null && !cacheList.isEmpty())
                    this.dataList.addAll(cacheList);
                    adapter.notifyDataSetChanged();
            } catch (Exception e) {
            }
        }
        update();
    }

    public void update() {
        if (firstLoad) {
            pageIndex = -1;
            showLoadingDialog();
            firstLoad = false;
        }
        getData(1, pageSize, TYPE_UPTATE);
    }


    public void getNextPage() {//获取下一页数据

        if (pageIndex == -1) {//数据是没用初始化成功过的
            update();
            return;
        }

        if (dataList.isEmpty()) {//如果没有数据，做刷新处理
            update();
            return;
        }

        if (dataList.size() % pageSize == 0) {//当前页是满的，需要获取下一页
            getData(pageIndex + 1, pageSize, TYPE_NEXT_PAGE);
        } else {//当前页未满，继续获取这页的数据
            getData(pageIndex, pageSize, TYPE_NEXT_PAGE);
        }
    }

    public void getDateFinish(List<T> list, int getDataType) {
        try {
            dismissLoadingDialog();

            if (getDataType == TYPE_UPTATE) {//更新数据结束
                if (list == null) {//更新失败

                } else if (list.isEmpty()) {//数据为空
                    if (noDateView != null)
                        try {
                            if (adapter instanceof BaseQuickAdapter){
                                ((BaseQuickAdapter)adapter).setEmptyView(noDateView);
                            }
                        } catch (Exception e) {
                        }
                    pageIndex = 0;
                    dataList.clear();
                    adapter.notifyDataSetChanged();
                } else {//更新成功
                    dataList.clear();
                    dataList.addAll(list);
                    //保存覆盖缓存数据
                    if (needCache) {
                        saveCache(list);
                    }

                    pageIndex = 1;
                    adapter.notifyDataSetChanged();
                }

            } else if (getDataType == TYPE_NEXT_PAGE) {//获取下一页数据结束
                if (list != null && !list.isEmpty()) {//获取下一页时。没有取到数据则不做处理，只有取到数据才处理

                    if (pageIndex == -1) {//列表初始状态，可能包含缓存数据，要清除，这里其实是不需要考虑这个情况的
                        dataList.clear();
                    }

                    //该页覆盖重复数据
                    int n = dataList.size() % pageSize;
                    for (int i = 0; i < n; i++) {
                        dataList.remove(dataList.size() - 1);
                    }
                    dataList.addAll(list);
                    pageIndex = dataList.size() / pageSize;
                    if (dataList.size() % pageSize > 0) {
                        pageIndex++;
                    }
                    adapter.notifyDataSetChanged();
                } else {
                    //ToastUtil.showTextToast("add数据为空");

                }
            }

        } catch (Exception e) {
        }
    }


    public abstract void showLoadingDialog();

    public abstract void dismissLoadingDialog();

    public abstract void saveCache(List<T> cacheData);

    public abstract List<T> getCache();

    View noDateView;

    public void setNoDateView(View noDate) {
        this.noDateView = noDate;

    }


}
